﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ISS.Models;
using System.IO;
using ISS.Business;
using System.Drawing;

namespace ISS.Controllers
{
    public class UploadImageController : Controller
    {
        //
        // GET: /UploadImage/

        [HttpPost]
        public String Index()
        {
            ImageDAO dao = new ImageDAO();

            if (Session["User"] != null)
            {
                if (Session["Album"] != null)
                {
                    Album album = (Album)Session["Album"];
                    if (Request.Files.Count > 0)
                    {
                        Users user = (Users)Session["User"];
                        if (!Directory.Exists(Server.MapPath("/Resource/" + user.UserId)))
                        {
                            Directory.CreateDirectory(Server.MapPath("/Resource/" + user.UserId));
                        }

                        if (!Directory.Exists(Server.MapPath("/Resource/" + user.UserId + "/" + album.AlbumId)))
                        {
                            Directory.CreateDirectory(Server.MapPath("/Resource/" + user.UserId + "/" + album.AlbumId));
                        }

                        if (!Directory.Exists(Server.MapPath("/Resource/" + user.UserId + "/" + album.AlbumId + "/Thumbnail")))
                        {
                            Directory.CreateDirectory(Server.MapPath("/Resource/" + user.UserId + "/" + album.AlbumId + "/Thumbnail"));
                        }

                        String path = Server.MapPath("/Resource/" + user.UserId + "/" + album.AlbumId);
                        String thumbnailPath = Server.MapPath("/Resource/" + user.UserId + "/" + album.AlbumId + "/Thumbnail");

                        HttpPostedFileBase file = null;
                        for (int i = 0; i < Request.Files.Count; i++ )
                        {
                            file = Request.Files[i];
                            DateTime time = DateTime.Now;
                            file.SaveAs(path + "\\" + time.ToFileTime() + "_" + file.FileName);

                            //Tao thumbnail
                            Image currentImage = Image.FromStream(file.InputStream);
                            Bitmap img1 = General.ResizeImage(currentImage, 250);
                            currentImage.Dispose();
                            String thumbnail1 = "";
                            try
                            {
                                General.saveJpeg(thumbnailPath + "\\" + time.ToFileTime() + "_" + file.FileName, img1, 75L);
                            }
                            catch (Exception ex)
                            {
                            }

                            Images image = new Images()
                                {
                                    AlbumId = album.AlbumId,
                                    Alias = General.ConvertToNoSign(file.FileName),
                                    CommentCount = 0,
                                    DirectLink = "/Resource/" + user.UserId + "/" + album.AlbumId + "/" + time.ToFileTime() + "_" + file.FileName,
                                    ImageThumbnail = "/Resource/" + user.UserId + "/" + album.AlbumId + "/Thumbnail/" + time.ToFileTime() + "_" + file.FileName,
                                    OriginalFrom = "",
                                    Like = 0,
                                    Title = file.FileName,
                                    UploadDate = DateTime.Now,
                                    ViewCount = 0
                                };
                            dao.Create(image);
                        }
                    }
                    Request.Files[0].SaveAs("G:\\" + Request.Files[0].FileName);
                    return "File was uploaded successfuly!";
                }
                else
                {

                }
            }
            else
            {

            }

            return "File was uploaded successfuly!";
        }
    }
}
